$(function(){
  //  alert(1)

  
  // 点击退出登录
  $('#logout').on('click',function(){
    layer.confirm('确定退出登录？', {icon: 3, title:'提示'}, function(index){
      //do something
      localStorage.removeItem('token')
      location.href='login.html'
      layer.close(index);
    })
  })
  //获取用户信息数据并渲染
  getUserInfo()

})
function getUserInfo(){
  $.ajax({
   method:'GET',
   url:'/my/userinfo',
   headers: {
      Authorization: localStorage.getItem('token')
   },
   success(res){
      //  console.log(res);
      if (res.status !== 0) {
          return layui.layer.msg(res.message,{icon:5})
      }
      // console.log(res);
      layui.layer.msg(res.message,{icon:6})
      renderAvatar(res.data)
   }
  })
}
function renderAvatar(user){
 var name = user.nickname || user.username
 if (user.user_pic !== null) {
     $('.layui-nav-img').show().attr('src',user.user_pic)
     $('.text-avatar').hide()
 }
 else {
  $('.layui-nav-img').hide()
  var first = name[0].toUpperCase()
  $('.text-avatar').show().html(first)
 }
 $('#welcome').html('欢迎'+name)
}